/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-02-06
 * Time: 11:05
 */
public class TestHeap {

    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
    }

    public void initElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }
    //不是核心代码，就传10个以内的数据，如果非要写，可以扩容拷贝

    //大根堆
    //时间复杂度 O(N)
    public void createHeap1() {
        for(int parent = (usedSize-1-1) / 2; parent >= 0; parent--) {
            shiftUp(parent,usedSize);
        }
    }

    public void shiftUp(int parent, int len) {
        int child = 2 * parent + 1;
        while(child < len) {
        if(child+1 < len && elem[child] < elem[child+1]) {
            child++;
        }
            if(elem[parent] < elem[child]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //小根堆
    public void createHeap2() {
        for(int parent = (usedSize-1-1) / 2; parent >= 0; parent--) {
            shiftDown(parent,usedSize);
        }
    }

    public void shiftDown(int parent,int len) {
        int child = 2 * parent + 1;
        while(child < len) {
            if(child+1 < len && elem[child] > elem[child+1]) {
                child++;
            }
            if(elem[parent] > elem[child]) {
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //插入
    public void offer(int val) {

    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

}
